package com.test.log4jperf;

import java.io.File;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Parameters for the log4j load test (standard output is switched off):
 * - count of concurrent logging threads
 * - lines count
 * - line length
 * - throwing exception
 * - dynamic logs (concatenating strings)
 * - is "is[level]Enabled" method used
 * 
 * - java version
 * - OS spec
 * - host machine spec
 * 
 * @author tomasz.sikora
 *
 */
public class LogSth {
    
	private final static Logger logger = Logger.getLogger(LogSth.class);

	private final static String HELLO_WORLD = "Hello World";
	
	public static void main(String[] args) {
        System.out.print("Prepare>>>");
		PropertyConfigurator.configure("log4j_01.properties");
		LogSth logSth = new LogSth();
        logSth.deleteLog();
		System.out.println("Done.");
        
		// the main load test
		System.out.print("Start>>>");
		long t0 = System.currentTimeMillis();
		for (int i = 0; i < 1000000; i++) {
			//logSth.log(HELLO_WORLD); // 12.797
			//logSth.logIsDebug(HELLO_WORLD); // 12.906
		    //logSth.log(HELLO_WORLD, Integer.toString(i)); // 14.0
            logSth.logIsDebug(HELLO_WORLD, Integer.toString(i)); // 13.75
		}
		System.out.println("Ended>>> " + (double)(System.currentTimeMillis() - t0)/1000);
		
		System.out.print("Cleaning up>>>");
		logSth.deleteLog();
		System.out.println("Finished.");
	}
	
	private boolean deleteLog() {
	    boolean success = (new File("example.log")).delete();
	    if (success) {
	        System.out.print("> log file deleted >");
	    } else {
            System.out.print("> log file not deleted >");
	    }
	    return success;
	}
	
	public void log(String string) {
	    logger.debug(string);
	}
	
    public void log(String string, String string2) {
        logger.debug(string + " - " + string2);
    }
	
	public void logIsDebug(String string) {
	    if (logger.isDebugEnabled()) {
            logger.debug(string);
        }
    }
    
	public void logIsDebug(String string, String string2) {
	    if (logger.isDebugEnabled()) {
	        logger.debug(string + " - " + string2);
	    }
	}
	
	public void foo2(int i) {
	    if (logger.isDebugEnabled()) {
		    logger.debug("Another message XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX nr: " + i);
	    }
		//logger.info("Another message nr: XXXXX");
		//logger.error("Stop that!", new Exception("The earth is getting warmer."));
	}
}
